gusucode.com > 各种VC自绘控件源码程序 > 各种VC自绘控件源码/code/SkinControls(自绘MFC基本控件 )/SkinControls/SkinControls/SkinRadio.cpp

    #include "stdafx.h"
#include "SkinRadio.h"

IMPLEMENT_DYNAMIC(CSkinRadio, CButton)

BEGIN_MESSAGE_MAP(CSkinRadio, CButton)
	ON_WM_CREATE()
	ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
	ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

CSkinRadio::CSkinRadio()
{
	m_bChecked = FALSE;
	m_crTextColor = RGB(0,0,0);
}

CSkinRadio::~CSkinRadio()
{
}


void CSkinRadio::PreSubclassWindow() 
{
	CButton::PreSubclassWindow();
	ModifyStyle(0, BS_OWNERDRAW);
	//ModifyStyleEx(0, WS_EX_TRANSPARENT);
}

//建立消息
int CSkinRadio::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (__super::OnCreate(lpCreateStruct)==-1) return -1;
	ModifyStyle(0, BS_OWNERDRAW);
	//ModifyStyleEx(0, WS_EX_TRANSPARENT);
	return 0;
}

HBRUSH CSkinRadio::CtlColor(CDC* pDC, UINT nCtlColor)
{
	pDC->SetBkMode(TRANSPARENT);
	return (HBRUSH)GetStockObject(NULL_BRUSH);
}

void CSkinRadio::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
	CRect rcItem(lpDIS->rcItem);

	pDC->SetBkMode(TRANSPARENT);

	int nCheckWidth = (int)m_ImageRadio.GetWidth()/4;
	int nCheckHeight = (int)m_ImageRadio.GetHeight();
	if (m_bChecked && this->IsWindowEnabled()) m_ImageRadio.Draw(pDC->m_hDC, rcItem.left, rcItem.Height()/2-nCheckHeight/2, nCheckWidth,nCheckHeight, 2*nCheckWidth, 0, nCheckWidth,nCheckHeight);
	else if (!this->IsWindowEnabled() && m_bChecked) m_ImageRadio.Draw(pDC->m_hDC, rcItem.left, rcItem.Height()/2-nCheckHeight/2, nCheckWidth,nCheckHeight, 3*nCheckWidth, 0, nCheckWidth,nCheckHeight);
	else if (this->IsWindowEnabled() && !m_bChecked) m_ImageRadio.Draw(pDC->m_hDC, rcItem.left, rcItem.Height()/2-nCheckHeight/2, nCheckWidth,nCheckHeight, 0, 0, nCheckWidth,nCheckHeight);
	else if (!this->IsWindowEnabled() && !m_bChecked) m_ImageRadio.Draw(pDC->m_hDC, rcItem.left, rcItem.Height()/2-nCheckHeight/2, nCheckWidth,nCheckHeight, nCheckWidth, 0, nCheckWidth,nCheckHeight);

	CString strText;
	GetWindowText(strText);
	CFont font;
	font.CreateFont(-12,0,0,0,400,0,0,0,134,3,2,1,2,TEXT("宋体"));
	pDC->SelectObject(&font);
	pDC->SetTextColor(m_crTextColor);
	pDC->TextOut(nCheckWidth+4, rcItem.Height()/2-nCheckHeight/2+1, strText);
	
	//if (lpDIS->itemState & ODS_FOCUS)
	//{
	//	int xlen = pDC->GetTextExtent(strText).cx;

	//	LOGBRUSH   lb; 
	//	lb.lbStyle = BS_SOLID;   
	//	lb.lbColor = RGB(0,0,0);   
	//	lb.lbHatch = 0;   

	//	DWORD styles[2] = {1, 1};   //点长为1,间隔为1 

	//	HPEN hPen = ExtCreatePen(PS_COSMETIC|PS_GEOMETRIC|PS_USERSTYLE|PS_ENDCAP_FLAT,   
	//		1, &lb, 2, styles);   

	//	CPoint TopLeft(nCheckWidth+3, rcItem.Height()/2-nCheckHeight/2);
	//	CPoint BottomRight(nCheckWidth+4+xlen, rcItem.Height()/2+nCheckHeight/2+1);
	//	CPoint TopRight(nCheckWidth+4+xlen, rcItem.Height()/2-nCheckHeight/2);
	//	CPoint BottomLeft(nCheckWidth+3, rcItem.Height()/2+nCheckHeight/2+1);
	//	CPen * pOldPen = pDC->SelectObject(CPen::FromHandle(hPen));
	//	pDC->MoveTo(TopLeft);
	//	pDC->LineTo(TopRight);
	//	pDC->MoveTo(TopLeft);
	//	pDC->LineTo(BottomLeft);

	//	pDC->MoveTo(BottomLeft.x, BottomLeft.y);
	//	pDC->LineTo(BottomRight.x, BottomRight.y);
	//	pDC->MoveTo(BottomRight.x, BottomRight.y);
	//	pDC->LineTo(TopRight.x, TopRight.y);
	//}
}

void CSkinRadio::SetRadioTextColor(COLORREF crTextColor)
{
	m_crTextColor = crTextColor;
}

void CSkinRadio::SetRadioImage(HRSRC hRes, DWORD imagetype, HMODULE hModule)
{
	m_ImageRadio.LoadResource(hRes, imagetype, hModule);
	m_ImageRadio.SetTransIndex(0);
	m_ImageRadio.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
}

void CSkinRadio::SetRadioImage(LPCTSTR pszFileName, DWORD imagetype)
{
	//效验参数
	ASSERT(pszFileName);
	if (pszFileName==NULL) return ;

	//加载位图
	m_ImageRadio.Load(pszFileName,imagetype);
	m_ImageRadio.SetTransIndex(0);
	m_ImageRadio.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
}


BOOL CSkinRadio::OnClicked() 
{
	SetChecked();
	return FALSE;
}

void CSkinRadio::SetChecked()
{
	m_bChecked = TRUE;
	UncheckOtherRadios();
	Invalidate(FALSE);
}

void CSkinRadio::UncheckOtherRadios()
{
	//这里可能会引发问题
	CWnd *pParent = GetParent();
	ASSERT(pParent);

	CWnd *pWnd = pParent->GetNextDlgGroupItem((CWnd *)this);
	while (pWnd && (pWnd != this))
	{
		if ((((CButton*)pWnd)->GetButtonStyle() & BS_AUTORADIOBUTTON))
		{
			((CSkinRadio *)pWnd)->m_bChecked = FALSE;
			((CSkinRadio *)pWnd)->Invalidate(FALSE);
		}
		pWnd = pParent->GetNextDlgGroupItem(pWnd);
	}
}


//////////////////////////////////////////////////////////////////////////